package com.currency.mall.util;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2018/12/27.
 */

public class TimeUtil {

    public static String addTime(String type, int day) {
        Format f = new SimpleDateFormat("yyyy-MM-dd");
        Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        if ("DAY".equals(type)) {
            c.add(Calendar.DATE, day);// 今天+1天
        } else if ("WEEK".equals(type)) {
            c.add(Calendar.DATE, day*7);// 今天+1天
        } else if ("MONTH".equals(type)) {
            c.add(Calendar.MONTH, day);// 今天+1天
        }
        return f.format(c.getTime());

    }

    public static String daysOfTwo(String startTime,String endTime) {
        long days = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //跨年不会出现问题
        //如果时间为：2016-03-18 11:59:59 和 2016-03-19 00:00:01的话差值为 0
        try {
            Date fDate = sdf.parse(startTime);
            Date oDate = sdf.parse(endTime);
            days = (oDate.getTime() - fDate.getTime()) / (1000 * 3600 * 24);
        }catch (Exception e){

        }

        return days+"";
    }

    public static String countdownText(String time){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
//            Date date=sdf.parse(time);
//            long  s1= date.getTime();
            long  s1 =Long.parseLong(time);
            long  s2= System.currentTimeMillis();//得到当前的毫秒
            long minusTime = s1 -s2;
            if(minusTime>0){
                int  day= (int) (minusTime/1000/60/60/24);
                long dayTime = day*60L*60*24*1000;
                long minusDay = minusTime - dayTime;
                int hour = (int) (minusDay/1000/60/60);
                long minusHour = minusDay - (hour*60L*60*1000);
                int minute = (int) (minusHour/1000/60);
                long minusMinute = minusHour - (minute*1000L*60);
                int second = (int) (minusMinute/1000);
                String sHour,sMinute,sSecond;
                sHour = hour<10?"0"+hour:""+hour;
                sMinute = minute<10?"0"+minute:""+minute;
                sSecond = second<10?"0"+second:""+second;
                if(day == 0){
                    return sHour+":"+sMinute+":"+sSecond ;
                }else{
                    return sHour+":"+sMinute+":"+sSecond ;
                }

            }else{
                return "00:00:00";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "00:00:00";
        }
    }

    //毫秒换成00:00:00
    public static String getCountTimeByLong(long finishTime) {
        int totalTime = (int) (finishTime / 1000);//秒
        int hour = 0, minute = 0, second = 0;

        if (3600 <= totalTime) {
            hour = totalTime / 3600;
            totalTime = totalTime - 3600 * hour;
        }
        if (60 <= totalTime) {
            minute = totalTime / 60;
            totalTime = totalTime - 60 * minute;
        }
        if (0 <= totalTime) {
            second = totalTime;
        }
        StringBuilder sb = new StringBuilder();

//        if (hour < 10) {
//            sb.append("0").append(hour).append(":");
//        } else {
//            sb.append(hour).append(":");
//        }
        if (minute < 10) {
            sb.append("0").append(minute).append(":");
        } else {
            sb.append(minute).append(":");
        }
        if (second < 10) {
            sb.append("0").append(second);
        } else {
            sb.append(second);
        }
        return sb.toString();

    }
}
